package com.p2p.Androidp2p;

import java.net.ServerSocket;
import java.net.Socket;

import android.util.Log;

/**
 * handles the multiple server connections and passes them to serverp2p class
 * for the transferring of files
 * @author Chago
 *
 */
public class ServerHandler implements Runnable
{
    ServerSocket server = null;
    public ServerHandler(ServerSocket s)
    {
	server = s;
    }
    /**
     * creates a new server and sits in a while loop constantly making connections
     * and passing them to worker threads
     */
    public void run()
    {
   	try 
   	{ 
             while(true)
            {
        	 Socket client = null;
        	 client = server.accept();
        	 Serverp2p dl = new Serverp2p(client);
        	 Thread download = new Thread(dl);
        	 download.start();
        	 Log.d("ANDROIDP2P S:handler", "Spawned worker thread");
             }
         
         } 
   	//general exceptions are caught here
   	catch (Exception e) 
         {
   	    Log.d("ANDROIDP2P S:handler", e.getMessage());
   	 Log.d("ANDROIDP2P S:handler", "ending handler");
   	    //System.exit(1);
         }  	
   	
    }

}
